package org.imaginedays.androidall.http;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.imaginedays.androidall.error.AndroidAllException;
import org.imaginedays.androidall.error.AndroidAllParseException;
import org.imaginedays.androidall.types.BaseType;
import org.imaginedays.androidall.types.UploadFile;
import org.imaginedays.androidall.types.User;
import org.imaginedays.androidall.xml.parsers.UserParser;

public class AndroidAllHttpApi {

	private static final Logger LOG = Logger.getLogger(AndroidAllHttpApi.class
			.getCanonicalName());

	private final DefaultHttpClient mHttpClient = AbstractHttpApi
			.createHttpClient();

	private HttpApi mHttpApi;

	private final String mApiBaseUrl;

	public AndroidAllHttpApi(String domain, String clientVersion,
			boolean userOAuth) {
		mApiBaseUrl = "https://" + domain + "/v1";
		if (userOAuth) {
			mHttpApi = new HttpApiWithOAuth(mHttpClient, clientVersion);
		} else {
			mHttpApi = new HttpApiWithBasicAuth(mHttpClient, clientVersion);
		}
	}

	/**
	 * HttpPost 请求
	 * 
	 * @param uId
	 * @param uName
	 * @param uAge
	 * @param uAddress
	 * @return
	 * @throws IOException
	 * @throws AndroidAllException
	 * @throws AndroidAllParseException
	 */
	public User addUserPost(String uId, String uName, String uAge,
			String uAddress) throws AndroidAllParseException,
			AndroidAllException, IOException {
		BasicNameValuePair uIdBNVP = new BasicNameValuePair("uId", uId);
		BasicNameValuePair uNameBNVP = new BasicNameValuePair("uName", uName);
		BasicNameValuePair uAgeBNVP = new BasicNameValuePair("uAge", uAge);
		BasicNameValuePair uAddressBNVP = new BasicNameValuePair("uAddress",
				uAddress);
		HttpPost httpPost = mHttpApi.createHttpPost(
				fullUrl(RequestServerApiUtils.URL_API_ADDUSER), uIdBNVP,
				uNameBNVP, uAgeBNVP, uAddressBNVP);
		return (User) mHttpApi.doHttpRequest(httpPost, new UserParser());
	}

	/**
	 * HttpGet 请求
	 * 
	 * @param uId
	 * @param uName
	 * @param uAge
	 * @param uAddress
	 * @return
	 * @throws AndroidAllParseException
	 * @throws AndroidAllException
	 * @throws IOException
	 */
	public User addUserGet(String uId, String uName, String uAge,
			String uAddress) throws AndroidAllParseException,
			AndroidAllException, IOException {
		BasicNameValuePair uIdBNVP = new BasicNameValuePair("uId", uId);
		BasicNameValuePair uNameBNVP = new BasicNameValuePair("uName", uName);
		BasicNameValuePair uAgeBNVP = new BasicNameValuePair("uAge", uAge);
		BasicNameValuePair uAddressBNVP = new BasicNameValuePair("uAddress",
				uAddress);
		HttpGet httpGet = mHttpApi.createHttpGet(
				fullUrl(RequestServerApiUtils.URL_API_ADDUSER), uIdBNVP,
				uNameBNVP, uAgeBNVP, uAddressBNVP);
		return (User) mHttpApi.doHttpRequest(httpGet, new UserParser());
	}

	private String fullUrl(String url) {
		return mApiBaseUrl + url;
	}

	// ////////////////////////////////////////////////////////////HttpURLConnection/////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * 带图片与参数的HttpURLConnection GET 请求
	 * 
	 * @param params
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public BaseType uploadUserInfo(Map<String, String> params, UploadFile file)
			throws Exception {
		return mHttpApi.accessServerByGetWithImg(
				RequestServerApiUtils.URL_API_ADD_USER_WITH_PHOTO, params,
				file, new UserParser());
	}

	/**
	 * 带参数的 HttpURLConnection GET 请求
	 * 
	 * @param params
	 * @return
	 */
	public User uploadUserInfo(HashMap<String, String> params) {
		return (User) mHttpApi.accessServerByGet(
				RequestServerApiUtils.URL_API_ADD_USER, params,
				new UserParser());
	}
	
	

}
